package com.lzw.util;
import java.io.*;
import java.net.*;
import android.content.*;
import android.net.*;
import android.util.*;
import android.widget.*;
import java.util.*;
import android.telephony.*;
import android.location.*;
import com.lzw.app.*;

public class myHttp
{
	public static final String GET = "GET";
	public static final String POST = "POST";
//	static final Map<String, String> head = new HashMap<>();
	public static void getInfo(final String url, final String request, final String requestMethod, final Map<String, String> head, final Listener l)
	{
		
		if (!isNetworkAvailable(myApp.getContext()))
		{
			Toast.makeText(myApp.getContext(), "网络连接不可用(>﹏<)", Toast.LENGTH_SHORT).show();
			return;
		}
		else{
			if (is3gNet(myApp.getContext()))
			Toast.makeText(myApp.getContext(), "当前正在使用流量-.-", Toast.LENGTH_SHORT).show();
		}
	sendRequest(url, request, requestMethod, head, l);
	}
	private static void sendRequest(final String url, final String request, final String requestMethod, final Map<String, String> head, final Listener l)
	{
		new Thread(){

			@Override
			public void run()
			{
				try
				{
					String strurl = url;
					switch(requestMethod){
						case "GET":
							strurl = url + "?" + request;
						break;
					}
					l.onDoing();
					HttpURLConnection a=null;
					StringBuffer f = new StringBuffer();
					URL u=new URL(strurl);
					a = (HttpURLConnection) u.openConnection();
					a.setReadTimeout(8000);
					a.setConnectTimeout(8000);
					a.setRequestMethod(requestMethod);
					a.setDoInput(true);
					Iterator<String> it = head.keySet().iterator();
					while(it.hasNext()){
						String key = it.next();
						String value = head.get(key);
						a.addRequestProperty(key, value);
					}			
					
					switch(requestMethod){
						case POST:
							a.setDoOutput(true);
							OutputStream os=a.getOutputStream();
							new DataOutputStream(os).writeBytes(request);
							break;
					}
				
					InputStream b=a.getInputStream();
					BufferedReader c=new BufferedReader(new InputStreamReader(b));
					String d;
					while ((d = c.readLine()) != null)
					{
						f.append(d);
					}
					if (a != null)
					{
						a.disconnect();
						l.onCorrect(f.toString());
					}

				}
				catch (Exception e)
				{
					l.onError(e);
				}
			}
		}.start();
	}
	
	public	interface Listener
	{
		void onDoing();
		void onCorrect(String re);
		void onError(Exception e);
	}


	// API都有只不过是将其进行综合并作为自己工程中的工具函数使用，很方便的。
	/**
	 * 获取当前网络状态的类型 *
	 * 
	 * @param mContext
	 * @return 返回网络类型
	 */
	public static final int NETWORK_TYPE_NONE = -0x1; // 断网情况
	public static final int NETWORK_TYPE_WIFI = 0x1; // WIFI模式
	public static final int NETWOKR_TYPE_MOBILE = 0x2; // GPRS模式

	public static int getCurrentNetType(Context mContext)
	{
		ConnectivityManager connManager = (ConnectivityManager) mContext
			.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo wifi = connManager
			.getNetworkInfo(ConnectivityManager.TYPE_WIFI); // WIFI
		NetworkInfo gprs = connManager
			.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); // GPRS

		if (wifi != null && wifi.getState() == NetworkInfo.State.CONNECTED)
		{
			//Log.d(TAG, "Current net type:  WIFI.");
			return NETWORK_TYPE_WIFI;
		}
		else if (gprs != null && gprs.getState() == NetworkInfo.State.CONNECTED)
		{
			//Log.d(TAG, "Current net type:  GPRS.");
			return NETWOKR_TYPE_MOBILE;
		}
		//Log.e(TAG, "Current net type:  NONE.");
		return NETWORK_TYPE_NONE;
	}

	/**
	 * 判断Android客户端网络是否连接
	 * 只能判断是否有可用的连接，而不能判断是否能连网
	 * @param context
	 * @return true/false
	 */
	public static boolean checkNet(Context context)
	{
		try
		{
			ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
			if (connectivity != null)
			{
				NetworkInfo info = connectivity.getActiveNetworkInfo();
				if (info != null && info.isConnected())
				{
					if (info.getState() == NetworkInfo.State.CONNECTED)
					{
						return true;
					}
				}
			}
		}
		catch (Exception e)
		{
			return false;
		}
		return false;
	}

	/**
	 * 检验网络连接 并toast提示 
	 * @return
	 */
	public static boolean noteIntentConnect(Context context)
	{
		ConnectivityManager con = (ConnectivityManager) context
			.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkinfo = con.getActiveNetworkInfo();
		if (networkinfo == null || !networkinfo.isAvailable())
		{
			// 当前网络不可用
			Toast.makeText(context.getApplicationContext(), "请先连接Internet！",
						   Toast.LENGTH_SHORT).show();
			return false;
		}
		boolean wifi = con.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
			.isConnectedOrConnecting();
		if (!wifi)
		{ // 提示使用WIFI
			Toast.makeText(context.getApplicationContext(), "建议您使用WIFI以减少流量！",
						   Toast.LENGTH_SHORT).show();
		}
		return true;

	}

	/**
	 * 判断网络连接是否可用
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isNetworkAvailable(Context context)
	{
		ConnectivityManager cm = (ConnectivityManager) context
			.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cm == null)
		{
		}
		else
		{
			// 如果仅仅是用来判断网络连接
			// 则可以使用 cm.getActiveNetworkInfo().isAvailable();
			NetworkInfo[] info = cm.getAllNetworkInfo();
			if (info != null)
			{
				for (int i = 0; i < info.length; i++)
				{
					if (info[i].getState() == NetworkInfo.State.CONNECTED)
					{
						return true;
					}
				}
			}
		}
		return false;
	}

    /**
	 * 判断网络状态是否可用
	 * @return true: 网络可用 ; false: 网络不可用
	 */
	public boolean isConnectInternet(Context mContext)
	{
		ConnectivityManager conManager = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = conManager.getActiveNetworkInfo();
		if (networkInfo != null)
		{ // 注意，这个判断一定要的，要不然会出错
			return networkInfo.isAvailable();
		}
		return false;
	}

	/**
	 * 判断GPS是否打开
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isGpsEnabled(Context context)
	{
		LocationManager lm = ((LocationManager) context
			.getSystemService(Context.LOCATION_SERVICE));
		List<String> accessibleProviders = lm.getProviders(true);
		return accessibleProviders != null && accessibleProviders.size() > 0;
	}

	/**
	 * 判断WIFI是否打开
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isWifiEnabled(Context context)
	{
		ConnectivityManager mgrConn = (ConnectivityManager) context
			.getSystemService(Context.CONNECTIVITY_SERVICE);
		TelephonyManager mgrTel = (TelephonyManager) context
			.getSystemService(Context.TELEPHONY_SERVICE);
		return ((mgrConn.getActiveNetworkInfo() != null && mgrConn
			.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel
			.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
	}

	/**
	 * 判断是否是3G网络
	 * 
	 * @param context
	 * @return
	 */
	public static boolean is3gNet(Context context)
	{
		ConnectivityManager cm = (ConnectivityManager) context
			.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkINfo = cm.getActiveNetworkInfo();
		if (networkINfo != null
			&& networkINfo.getType() == ConnectivityManager.TYPE_MOBILE)
		{
			return true;
		}
		return false;
	}

	/**
	 * 判断是否是WIFI网络
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isWifiNet(Context context)
	{
		ConnectivityManager cm = (ConnectivityManager) context
			.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkINfo = cm.getActiveNetworkInfo();
		if (networkINfo != null
			&& networkINfo.getType() == ConnectivityManager.TYPE_WIFI)
		{
			return true;
		}
		return false;
	}

}
